package com.xin.controller;
import com.xin.annoations.Permission;
import com.xin.constants.CommonResult;
import com.xin.entity.crm.DeptEntity;
import com.xin.service.DeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
 * 部门
 */
@RestController
@RequestMapping("/dept")
@Permission(value = "部门信息",sn="dept")
@Api(tags = "部门相关", description = "部门")
public class DeptController {
    @Resource
    private DeptService deptService;

    /**
     * 添加部门信息
     * @param deptEntity
     * @return
     */
    @PostMapping("/add")
    @Permission(value = "添加部门信息",sn="dept:add")
    @ApiOperation(value = "添加部门信息")
    public CommonResult<?> add(@RequestBody DeptEntity deptEntity){
       return this.deptService.add(deptEntity)>=1?CommonResult.success(null,"部门添加成功"):CommonResult.error(499,"网络异常");
    }


    /**
     * 查看所有部门信息
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/findList")
    @Permission(value = "分页查看所有部门信息",sn="dept:findList")
    @ApiOperation(value = "分页查看所有部门信息")
    public CommonResult<?> findList(Integer page,Integer size){
        return CommonResult.success(this.deptService.findList(page,size),"请求成功");
    }

    /**
     * 根据部门名称模糊查询部门信息
     * @param name
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/findByNameLike")
    @Permission(value = "查看所有部门信息分页并模糊查询",sn="dept:findByNameLike")
    @ApiOperation(value = "查看所有部门信息分页并模糊查询")
    public CommonResult<?> findByNameLike(String name,Integer page,Integer size){
        return CommonResult.success(this.deptService.findByNameLike(name,page,size),"请求成功");
    }


    /**
     * 修改部门信息
     * @param deptEntity
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改部门信息")
    @Permission(value = "修改部门信息",sn="dept:update")
    public CommonResult<?> update(@RequestBody DeptEntity deptEntity){

        return this.deptService.update(deptEntity)>=1?CommonResult.success(null,"修改部门成功"):CommonResult.error(499,"修改部门失败");
    }

    /**
     * 删除部门信息
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除部门信息")
    @Permission(value = "删除部门信息",sn="dept:delete")
    public CommonResult<?> delete(Long id){
        return this.deptService.delete(id)>0?CommonResult.success(null,"部门删除成功"):CommonResult.error(499,"网络异常");
    }

}
